<?php


namespace app\command;


use app\common\CacheKey;
use app\services\utils\RedisService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class PutCupboardCoordinateToRedis extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('put_cupboard_coordinate_to_redis')->setDescription('把柜机坐标存入redis geo');
    }

    protected function execute(Input $input, Output $output)
    {
        $operatorIdArr = Db::connect('cupboard')
            ->table('operator')
            ->column('id');
        $time = date('Y-m-d 00:00:00');
        foreach ($operatorIdArr as $value) {
            $cupboards = Db::connect('cupboard')
                ->table('cupboard')
                ->where('operator_id', $value)
                ->whereNotNull('lat')
                ->whereNotNull('lng')
                ->where('updated_at', '>=', $time)
                ->field('id,lat,lng')
                ->order('id', 'desc')
                ->select()
                ->toArray();
            if (!$cupboards) {
                continue;
            }
            $redis = RedisService::instance();
            $cacheKey = sprintf(CacheKey::OPERATOR_CUPBOARD_LIST, $value);
            foreach ($cupboards as $item) {
                $res = $redis->geopos($cacheKey, $item['id']);
                if ($res[0]) {
                    continue;
                }
                $redis->geoadd($cacheKey, $item['lng'], $item['lat'], $item['id']);
            }
        }
        $output->writeln('write over!!!');
    }
}